package com.sduept.nwld.dataserver.controller.ledger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

import org.primefaces.PrimeFaces;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.cim.ledger.excetpion.ManagementUnitException;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.ledger.services.ManagementUnitService;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.PingYinUtil;

@Named
@SessionScoped
public class LedgerQueryDataController implements Serializable {

	private static final long serialVersionUID = -4029463799959859507L;

	@Autowired
	private CimManager cimM;
	@Autowired
	private ManagementUnitService unitService;
	
	/**
	 * 调管单位
	 */
	private List<ManagementUnitDict> dispatchUnitList;
	/**
	 * 运维单位
	 */
	private List<ManagementUnitDict>  maintenanceUnitList;
	/**
	 * 检修范围
	 */
	private List<ManagementUnitDict> primaryAndSecondaryScopeList;
	/**
	 * 设备资产归属
	 */
	private List<String> assetOwnerList;
	private List<String> unitTypeList = new ArrayList<String>();
	
	private List<VoltageVO> standVoltageLevels = new ArrayList<VoltageVO>();
	
	@PostConstruct
	public void init(){
		try {
			dispatchUnitList = unitService.getAllDispatchingManagements();
			maintenanceUnitList = unitService.getAllMaintenanceManagements();
			primaryAndSecondaryScopeList = unitService.getAllMaintenanceScopes();
		} catch (ManagementUnitException e) {
			e.printStackTrace();
		}
		assetOwnerList = Arrays.asList("南网", "发电厂", "直供电用户");
		unitTypeList = Arrays.asList("供电局","超高压分局","发电厂","直供电用户");
		getStandVolLevels();
	}
	
	/**
	 * 资产归属，查询时增加“全部”和“未设置”两个选项
	 */
	public List<String> completeAssetOwner(String query) {
		List<String> result = completeAssetOwnerWhenEdit(query);
		result.add(0, "全部");
		result.add(1, "未设置");
		return result;
	}
	
	/**
	 * 调管单位，查询时增加“全部”和“未设置”两个选项
	 */
	public List<String> completeDispatchUnit(String query) {
		List<String> result = new ArrayList<String>();
		for (ManagementUnitDict dept : dispatchUnitList) {
			String pName = PingYinUtil.getPingYinHeader(dept.getName()); 
			if (pName.contains(query)) {
				result.add(dept.getName());
			}
		}
		result.add(0, "全部");
		result.add(1, "未设置");
		return result;
	}

	/**
	 * 运维单位，查询时增加“全部”和“未设置”两个选项
	 */
	public List<String> completeMaintenanceUnit(String query) {
		List<String> result = new ArrayList<String>();
		for (ManagementUnitDict dept : maintenanceUnitList) {
			String pName = PingYinUtil.getPingYinHeader(dept.getName());
			if(pName.contains(query)) {
				result.add(dept.getName());
			}
		}
		result.add(0, "全部");
		result.add(1, "未设置");
		return result;
	}

	/**
	 * 一二次设备检修范围，查询时增加“全部”和“未设置”两个选项
	 */
	public List<String> completePrimaryAndSecondaryScope(String query) {
		List<String> result = new ArrayList<String>();
		for (ManagementUnitDict dept : primaryAndSecondaryScopeList) {
			String pName = PingYinUtil.getPingYinHeader(dept.getName());
			if(pName.contains(query)) {
				result.add(dept.getName());
			}
		}
		result.add(0, "全部");
		result.add(1, "未设置");
		return result;
	}
	
	/**
	 * 获取标准电压等级（实际上其他就是电压等级为0）
	 */
	private void getStandVolLevels() {
		boolean flag = false;
		String labelSuffix = "kV";
		List<Float> vols = cimM.findStationStandVolLevels();
		for (Float vol : vols) {
			String volStr = String.valueOf(vol.intValue());
			if(vol.intValue() != 0) {
				standVoltageLevels.add(new VoltageVO(volStr+labelSuffix, volStr));
			} else {
				flag = true;
			}
		}
		if(true == flag) {
			standVoltageLevels.add(new VoltageVO("其他", "0"));
		}
	}

	/**
	 * 单位类型,编辑时用
	 */
	public List<String> completeUnitType(String query) {
		List<String> result = new ArrayList<String>();
		for (String string : unitTypeList) {
			String pName = PingYinUtil.getPingYinHeader(string);
			if (pName.contains(query)) {
				result.add(string);
			}
		}
		return result;
	}

	/**
	 * 资产归属，编辑时调用
	 */
	public List<String> completeAssetOwnerWhenEdit(String query) {
		List<String> result = new ArrayList<String>();
		for (String string : assetOwnerList) {
			String pName = PingYinUtil.getPingYinHeader(string);
			if (pName.contains(query)) {
				result.add(string);
			}
		}
		return result;
	}
	
	/**
	 * 打开选择调度部门弹出框
	 */
	public void getDesorgDepartDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 620);
		options.put("height", 650);
		Map<String, List<String>> params = new HashMap<>();
//		openDialog("/ui/common/department_selector_desorg.xhtml",  params,options);
		PrimeFaces.current().dialog().openDynamic("/ui/common/department_selector_desorg.xhtml",  options,params);
	}
	
	/**
	 * 打开选择运维部门弹出框
	 */
	public void getOpDepartDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 620);
		options.put("height", 650);
		Map<String, List<String>> params = new HashMap<>();
//		openDialog("/ui/common/department_selector_opSubCompany.xhtml",  params,options);
		PrimeFaces.current().dialog().openDynamic("/ui/common/department_selector_opSubCompany.xhtml",  options,params);
	}
	
	/**
	 * 打开一二次设备检修范围弹出框
	 */
	public void getPrimaryAndSecondaryScopeSelectDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 620);
		options.put("height", 650);
		Map<String, List<String>> params = new HashMap<>();
//		openDialog("/ui/common/department_selector_primaryAndSecondaryScrope.xhtml",  params,options);
		PrimeFaces.current().dialog().openDynamic("/ui/common/department_selector_primaryAndSecondaryScrope.xhtml",  options,params);
	}
	
	/**
	 * 打开选择变电站弹出框
	 */
	public void getSubstaionSelectDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 620);
		options.put("height", 650);
		Map<String, List<String>> params = new HashMap<>();
//		openDialog("/ui/common/subStation_selector_groupByDesorg.xhtml",  params,options);
		PrimeFaces.current().dialog().openDynamic("/ui/common/subStation_selector_groupByDesorg.xhtml",  options,params);
	}

	public List<VoltageVO> getStandVoltageLevels() {
		return standVoltageLevels;
	}

	public void setStandVoltageLevels(List<VoltageVO> standVoltageLevels) {
		this.standVoltageLevels = standVoltageLevels;
	}

	public class VoltageVO {
		public String label;
		public String value;
		public VoltageVO(String label,String value) {
			this.label = label;
			this.value = value;
		}
		public String getLabel() {
			return label;
		}
		public void setLabel(String label) {
			this.label = label;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
	}
}
